package br.com.etecmatao.admin.controller;

import br.com.etecmatao.business.BusinessLocator;
import br.com.etecmatao.business.impl.BusinessLocatorImpl;
import br.com.etecmatao.model.Professores;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
public class FormProfessor {

    private Professores professor;
    private BusinessLocator businessLocator;
    private DataModel<Professores> lista;

    public DataModel<Professores> getLista() {
        lista = new ListDataModel<Professores>(businessLocator.getProfessoresBusiness().finByAll());
        return lista;
    }

    public void setLista(DataModel<Professores> lista) {
        this.lista = lista;
    }

    public FormProfessor() {
        professor = new Professores();
        businessLocator = new BusinessLocatorImpl();
    }

    public Professores getProfessor() {
        return professor;
    }

    public void setProfessor(Professores professor) {
        this.professor = professor;
    }

    public void save() {
        professor.setLogin((br.com.etecmatao.model.Login) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user"));
        professor.setStatus("A");
        if (professor.getIdProfessores() != null) {
            businessLocator.getProfessoresBusiness().update(professor);
        } else {
            businessLocator.getProfessoresBusiness().save(professor);
        }
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Cadastro realizado com sucesso"));
    }

    public void delete(){
        professor = lista.getRowData();
        businessLocator.getProfessoresBusiness().delete(professor);
    }

    public String editar(){
        professor = lista.getRowData();
        return "formProfessor";
    }

    public String create(){
        professor = new Professores();
        return "formProfessor";
    }
}
